<?php
/**
 * 状态模块
 * 
 * @version $Id$
 */

class Module_status
{
	/**
	 * 构造函数
	 */
	function __construct() {
		
	}

	
	function index() {
		return $this->showlist();
	}
	
	/**
	 * 状态列表
	 */
	function showlist() {
		$uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		
		// 如果没有uin参数，则显示自己的个人主页
		if(empty($uin)) {
			$uin = SessionInfo('uin');
		}
		
		$smarty = loadSmarty();
		// 获取用户数据,用于判断uin用户内容是否存在
		loadLib('User');
		$userinfo = USER::getFullInfo($uin);
		if(empty($userinfo)){
			return View::error('该用户信息不存在' );
		}
		// 获取数据
		loadLib('Status');
		$status_list = Status::getList($uin);
		//print_r($status_list);
		
		$send_uin_list = array();
		
		foreach($status_list as $row) {
			$send_uin_list[] = $row['uin'];
			
			if(!empty($row['reply'])) {
				foreach($row['reply'] as $reply_row) {
					$send_uin_list[] = $reply_row['uin'];
				}
			}
		}
		
		if(!empty($send_uin_list)) {
			loadLib('User');
			$send_uin_info = User::getBaseInfo($send_uin_list);
			
			foreach($status_list as $k => $row) {
				if(!empty($row['reply'])) {
					foreach($row['reply'] as $j => $reply_row) {
						$reply_uin = $reply_row['uin'];	
						$status_list[$k]['reply'][$j]['name'] = isset($send_uin_info[$reply_uin]) ? $send_uin_info[$reply_uin]['name'] : '';
						$status_list[$k]['reply'][$j]['sex'] = isset($send_uin_info[$reply_uin]) ? $send_uin_info[$reply_uin]['sex'] : '';
					}
				}
			}
			
		}
		//print_r($status_list);
		$smarty->assign('status_list' , $status_list);
		
		// 准备个人主页导航数据
		View::prepare_profile_nav_data($uin);

		return $smarty->fetch('status/showlist.tpl.php');
	}
	
	/**
	 * 写状态
	 */
	function add() {
		$status = isset($_POST['status']) ? trim($_POST['status']) : null;
		
		// 检查状态有效性
		if($status === null || strlen($status) == 0) {
			return View::error('没有填写内容');
		}
		
		// 长度校验
		if(strlen($status) > 255) {
			return View::error('内容超过了长度限制，最长是140个字');
		}
		
		$uin = SessionInfo('uin');
		
		loadLib('Status');
		$code = Status::add($uin , $status);
		
		if($code === 0) {
			// 写feed
			loadLib('Feed');
			Feed::addStatus($uin , Status::$last_insert_id , $status);
			loadLib('Operate');
			Operate::getAdd($uin , 0 , 'status');
			return View::success('写心情成功');
		} else {
			return View::error('系统繁忙，请稍后再试' , $code);
		}
	}
	/**
	*回复界面
	*/
	function send(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : null;
		$uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$smarty = loadSmarty();
		if(empty($id) || empty($uin)){
			return View::error('参数错误！' );
		}
		//var_dump($id,$uin);
		// 获取心情回复数据
		loadLib('Status');
		//获取数据
		$status_send = Status::getOneStatus($uin , $id);
		//print_r($Status_send);
		//print_r($status_send);
		if(empty($status_send['uin'])){
			return View::error('该信息内容不存在' );
		}
		//var_dump($status_send);
		$send_uin_list = array();
		$send_uin_list[] = $status_send['uin'];
		//var_dump($Status_send);
		if(!empty($status_send['reply'])) {
			foreach($status_send['reply'] as $reply_row) {
				$send_uin_list[] = $reply_row['uin'];
			}
		}
			
		if(!empty($send_uin_list)) {
			loadLib('User');
			$send_uin_info = User::getBaseInfo($send_uin_list);
				
			$send_uin = $status_send['uin'];
				
			$status_send['send_uin_name'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['name'] : '';
			$status_send['send_uin_sex'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['sex'] : '';
			
			if(!empty($status_send['reply'])) {
				foreach($status_send['reply'] as $j => $reply_row) {
					$reply_uin = $reply_row['uin'];	
					$status_send['reply'][$j]['name'] = isset($send_uin_info[$reply_uin]) ? $send_uin_info[$reply_uin]['name'] : '';
					$status_send['reply'][$j]['sex'] = isset($send_uin_info[$reply_uin]) ? $send_uin_info[$reply_uin]['sex'] : '';
				}
			}
		}
	//print_r($Status_send);
	$smarty->assign('status_send',$status_send);
	$smarty->assign('id',$id);
	$smarty->assign('uin',$uin);
	return $smarty->fetch('status/send.tpl.php');
	}
	/**
	 * 心情回复操作
	 */
	function upsend(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$others_uin = isset($_POST['others_uin']) ? intval($_POST['others_uin']) : null;
		$content = isset($_POST['content']) ? trim($_POST['content']) : null;
		if(empty($id)) {
			return View::error('参数错误' );
		}
		
		// 检查是否有内容
		if($content === null || strlen($content) == 0) {
			return View::error('内容不能为空');
		}
		
		// 长度校验
		if(strlen($content) > 255) {
			return View::error('内容超过了长度限制，最长是140个字');
		}

		$uin = SessionInfo('uin');
		
		loadLib('Status');
		
		$org_status = Status::getOneStatus($others_uin , $id);
		if(empty($org_status)) {
			return View::error('该信息不存在' , $code);
		}
		
		$code = Status::getUpSend($others_uin , $uin , $id , $content , $org_status);
		//print_r($org_feedback);
		if($code === 0) {
			
			if($uin != $org_status['uin']) {
				loadLib('Operate');
				Operate::getAdd($uin , $others_uin , 'status_reply');
				$sysmsg_to_uin = $org_status['uin'];
				loadLib('SysMsg');
				SysMsg::sendSystem($sysmsg_to_uin , array(
					'send_uin' => $uin,
					'id' => $id,
				) , 1010);
			}		
			return View::success('回复心情成功');
		} else {
			return View::error('该信息已删除，操作失败' , $code);
		}
	}
}

# end of script
